package com.zxl.always.on.display;

import android.app.Application;
import android.content.Context;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;

import com.alibaba.sdk.android.feedback.impl.FeedbackAPI;
import com.zxl.always.on.display.configs.AdsConfigsHelper;
import com.zxl.always.on.display.constant.LockerEnv;
import com.zxl.always.on.display.constant.LockerSetting;
import com.zxl.always.on.display.tools.UserStartManager;
import com.zxl.always.on.display.track.MobclickAgentHelper;
import com.zxl.always.on.display.ui.receiver.CoreReceiver;
import com.zxl.always.on.display.ui.receiver.ScreenReceiver;
import com.zxl.always.on.display.ui.service.CoreService;
import com.zxl.always.on.display.ui.service.ScreenService;
import com.zxl.process.sdk.DaemonClient;
import com.zxl.process.sdk.DaemonConfigurations;

/**
 * Created by Administrator on 2018/2/26.
 */

public class DisplayApp extends Application {

    private static HandlerThread sWorkerThread = new HandlerThread("privacy-main");

    static {
        sWorkerThread.start();
    }

    private static Handler sWorker = new Handler(sWorkerThread.getLooper());

    private static Handler sHandler = new Handler(Looper.getMainLooper());

    @Override
    public void onCreate() {
        super.onCreate();
        UserStartManager.refreshState(this);
        LockerSetting.init();
        MobclickAgentHelper.init(this);
        AdsConfigsHelper.refreshControlInfo();
        ActivityLifecycle.registerActivityLifecycleCallbacks(this);
        ScreenService.startCoreService(this);
        FeedbackAPI.init(this, "24840722", "d4d9ec5c9986e86400a9169f4dd4aeda");
    }

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        ApplicationHelper.setUp(base, sWorker, sHandler);
        DaemonClient.getInstance().init(createDaemonConfigurations());
        DaemonClient.getInstance().onAttachBaseContext(base);
    }

    /**
     * 构建守护配置
     * @return
     */
    private DaemonConfigurations createDaemonConfigurations() {
        DaemonConfigurations configs = new DaemonConfigurations(new DaemonConfigurations.DaemonConfiguration(LockerEnv.Process.SCREEN_PROCESS,
                ScreenService.class.getCanonicalName(), ScreenReceiver.class.getCanonicalName()), new DaemonConfigurations.DaemonConfiguration(
                LockerEnv.Process.CORE_PROCESS, CoreService.class.getCanonicalName(), CoreReceiver.class.getCanonicalName()));
        //开启守护效果统计
        configs.setStatisticsDaemonEffect(true);
        //设置唤醒常驻服务轮询时长
        configs.setDaemonWatchInterval(20);
        //设置唤醒常驻服务轮询时长(android版本21以上)
        configs.setDaemonWatchIntervalAboveAPI21(20);
        //设置多长时间后，第一次唤醒其它常驻进程
        configs.setDaemonOtherPersistentProcessTrigger(2);
        //设置唤醒其它常驻进程的监控时间间隔
//        configs.setDaemonOtherPersistentProcessInterval(60);
        return configs;
    }
}
